﻿using System;
using System.Web.Script.Serialization;
using WebStickyNotes.Contracts;
using WebStickyNotes.Contracts.Interfaces;

namespace WebStickyNotes.Helpers
{
    public static class AuditHelper
    {
        public static void AuditAction(IWebStickyNotesDataAccess dataAccess, string description, int userId)
        {
            var taskAudit = new TaskAudit
                                {
                                    Title = description,
                                    AuditDate = DateTime.Now,
                                    AuditFor = "Misc",
                                    ItemId = 0,
                                    TaskPersonId = userId,
                                    ItemAfter = string.Empty
                                };

            dataAccess.SaveTaskAudit(taskAudit);
        }

        public static void AuditAction(IWebStickyNotesDataAccess dataAccess, string description, TaskDetail objectToAudit, int userId)
        {
            var taskAudit = new TaskAudit
                                {
                                    Title = description,
                                    AuditDate = DateTime.Now,
                                    AuditFor = "TaskDetail",
                                    ItemId = objectToAudit.Id,
                                    TaskPersonId = userId,
                                    ItemAfter = SerializeToJson(objectToAudit)
                                };

            dataAccess.SaveTaskAudit(taskAudit);
        }

        public static void AuditAction(IWebStickyNotesDataAccess dataAccess, string description, TaskStream objectToAudit, int userId)
        {
            var taskAudit = new TaskAudit
                                {
                                    Title = description,
                                    AuditDate = DateTime.Now,
                                    AuditFor = "TaskStream",
                                    ItemId = objectToAudit.Id,
                                    TaskPersonId = userId,
                                    ItemAfter = SerializeToJson(objectToAudit)
                                };

            dataAccess.SaveTaskAudit(taskAudit);
        }

        public static void AuditAction(IWebStickyNotesDataAccess dataAccess, string description, TaskProject objectToAudit, int userId)
        {
            var taskAudit = new TaskAudit
                                {
                                    Title = description,
                                    AuditDate = DateTime.Now,
                                    AuditFor = "TaskProject",
                                    ItemId = objectToAudit.Id,
                                    TaskPersonId = userId,
                                    ItemAfter = SerializeToJson(objectToAudit)
                                };

            dataAccess.SaveTaskAudit(taskAudit);
        }

        public static void AuditAction(IWebStickyNotesDataAccess dataAccess, string description, TaskPriority objectToAudit, int userId)
        {
            var taskAudit = new TaskAudit
                                {
                                    Title = description,
                                    AuditDate = DateTime.Now,
                                    AuditFor = "TaskPriority",
                                    ItemId = objectToAudit.Id,
                                    TaskPersonId = userId,
                                    ItemAfter = SerializeToJson(objectToAudit)
                                };

            dataAccess.SaveTaskAudit(taskAudit);
        }

        public static void AuditAction(IWebStickyNotesDataAccess dataAccess, string description, TaskPerson objectToAudit, int userId)
        {
            var taskAudit = new TaskAudit
                                {
                                    Title = description,
                                    AuditDate = DateTime.Now,
                                    AuditFor = "TaskPerson",
                                    ItemId = objectToAudit.Id,
                                    TaskPersonId = userId,
                                    ItemAfter = SerializeToJson(objectToAudit)
                                };

            dataAccess.SaveTaskAudit(taskAudit);
        }

        private static string SerializeToJson(object objectToSerialize)
        {
            var serializer = new JavaScriptSerializer();
            return serializer.Serialize(objectToSerialize);
        }
    }
}